﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clouds : MonoBehaviour {

	public List<BgObj> cloudList;
	public float moveSpeed;

	void Start () {
		int count = cloudList.Count;
		for (var i = 0; i < count; i++) {
			var cloud = cloudList [i];
			if (i == 0) {
				cloud.offPreCloud = Random.Range (4, 8);
			} else {
				var pre = cloudList [i - 1];
				cloud.offPreCloud = cloud.transform.position.x - pre.transform.position.x;
			}
			cloud.GetComponent<SpriteRenderer> ().sortingOrder = -4;
			cloud.canFloat = false;
		}
	}
	
	void Update () {
		for (var i = 0; i < cloudList.Count; i++) {
			var obj = cloudList [i];
			obj.transform.position = new Vector3 (obj.transform.position.x - moveSpeed*Time.deltaTime, obj.transform.position.y, obj.transform.position.z);
		}
		var obj1 = cloudList [0];
		var camera = Camera.main.GetComponent<CameraMoved> ();
		if (camera.OutLeft (obj1.transform.position.x + obj1.width/2)) {
			var last = cloudList [cloudList.Count - 1];
			obj1.transform.position = new Vector3 (last.transform.position.x + obj1.offPreCloud, obj1.transform.position.y, obj1.transform.position.z);
			cloudList.Remove (obj1);
			cloudList.Add (obj1);
		}
	}

}
 